ECHO = @echo
OUTNAME = trtrun
CC := g++
CUCC := nvcc
SRCDIR := src
OBJDIR := objs
#LEAN := /home/fly/lean
#BINDIR := $(LEAN)/tensorRTIntegrate
BINDIR := workspace

#TENSORRT_NAME := ubuntu-TensorRT/TensorRT-7.0.0.11
TENSORRT_INCLUDE_PATH := /usr/include/aarch64-linux-gnu
TENSORRT_LIB_PATH := /usr/lib/aarch64-linux-gnu
#TENSORRT_NAME := TensorRT-6.0.1.8-cuda10.2-cudnn7.6
#CFLAGS := -std=c++17 -fPIC -g -O3 -fopenmp -w -DONNX_ML -DNDEBUG 
# CUFLAGS := -std=c++17 -Xcompiler -fPIC -g -O3 -w -gencode=arch=compute_75,code=sm_75 -gencode=arch=compute_61,code=sm_61
CXXFLAGS := -std=c++14 -fPIC -g -O3 -fopenmp -w -DONNX_ML -DNDEBUG 
#CUFLAGS := -std=c++14 -Xcompiler -fPIC -g -O3 -w -gencode=arch=compute_75,code=sm_75 -gencode=arch=compute_61,code=sm_61
CUFLAGS := -std=c++14 -Xcompiler -fPIC -g -O3 -w -gencode=arch=compute_53,code=sm_53
#INC_OPENCV := /usr/local/opencv346-arm/include
INC_OPENCV := /usr/include/opencv4
#INC_LOCAL := ./src ./src/builder ./src/common ./src/infer ./src/plugin ./src/plugin/plugins
INC_LOCAL := ./src ./src/builder ./src/caffeplugin ./src/caffeplugin/plugins ./src/common \
 ./src/infer ./src/onnx ./src/onnx_parser ./src/onnxplugin ./src/onnxplugin/plugins \
  ./src/analyse ./src/analyse/fonts ./src/analyse/models ./src/analyse/common
# INC_SYS := $(LEAN)/protobuf3.11.4/include
INC_SYS := /usr/local/include
INC_EIGEN := /usr/include/eigen3
# INC_FREETYPE := /usr/local/freetype/include/freetype2
INC_FREETYPE := /usr/local/include/freetype2
INC_CUDA := /usr/local/cuda/include $(TENSORRT_INCLUDE_PATH)
INCS := $(INC_SYS) $(INC_OPENCV) $(INC_LOCAL) $(INC_CUDA) $(INC_EIGEN) $(INC_FREETYPE)
INCS := $(foreach inc, $(INCS), -I$(inc))

LIB_CUDA := /usr/local/cuda/lib64 $(TENSORRT_LIB_PATH)
# LIB_SYS := $(LEAN)/protobuf3.11.4/lib
LIB_SYS := /usr/local/lib
#LIB_OPENCV := /usr/local/opencv346-arm/lib 
LIB_OPENCV := /usr/lib/aarch64-linux-gnu
LIB_FREETYPE := /usr/local/freetype/lib
LIBS := $(LIB_SYS) $(LIB_CUDA) $(LIB_OPENCV) $(LIB_FREETYPE)
LIBS := $(foreach lib, $(LIBS),-L$(lib))

RPATH := $(LIB_SYS) $(LIB_CUDA) $(LIB_OPENCV)
RPATH := $(foreach lib, $(RPATH),-Wl,-rpath=$(lib))

LD_OPENCV := opencv_core opencv_highgui opencv_imgproc opencv_video opencv_videoio opencv_imgcodecs
LD_NVINFER := nvinfer nvinfer_plugin nvparsers
LD_CUDA := cuda curand cublas cudart cudnn
LD_FREETYPE := freetype
LD_SYS := stdc++
LDS := $(LD_SYS) $(LD_OPENCV) $(LD_NVINFER) $(LD_CUDA) $(LD_FREETYPE)
LDS := $(foreach lib, $(LDS), -l$(lib))

SRCS := $(shell cd $(SRCDIR) && find -name "*.cpp")
OBJS := $(patsubst %.cpp,%.o,$(SRCS))
OBJS := $(foreach item,$(OBJS),$(OBJDIR)/$(item))
CUS := $(shell cd $(SRCDIR) && find -name "*.cu")
CUOBJS := $(patsubst %.cu,%.o,$(CUS))
CUOBJS := $(foreach item,$(CUOBJS),$(OBJDIR)/$(item))
OBJS := $(subst /./,/,$(OBJS))
CUOBJS := $(subst /./,/,$(CUOBJS))

all: $(BINDIR)/$(OUTNAME)
	$(ECHO) Done, now you can run this program with \"make run\" command.

run: all
	@cd $(BINDIR) && ./$(OUTNAME)

$(BINDIR)/$(OUTNAME): $(OBJS) $(CUOBJS)
	$(ECHO) Linking: $@
	@g++ -fopenmp $(LIBS) -o $@ $^ $(LDS) $(RPATH) -pthread -lprotobuf

$(CUOBJS) : $(OBJDIR)/%.o : $(SRCDIR)/%.cu
	@if [ ! -d $@ ]; then mkdir -p $(dir $@); fi
	$(ECHO) Compiling: $<
	@$(CUCC) $(CUFLAGS) $(INCS) -c -o $@ $<

$(OBJS) : $(OBJDIR)/%.o : $(SRCDIR)/%.cpp
	@if [ ! -d $@ ]; then mkdir -p $(dir $@); fi
	$(ECHO) Compiling: $<
	@$(CC) $(CXXFLAGS) $(INCS) -c -o $@ $<

clean:
	rm -rf $(OBJDIR) $(BINDIR)/$(OUTNAME)

pfld: $(OBJS) $(CUOBJS)
	@g++ -fPIC -shared -fopenmp $(LIBS) -o libpfld.so $^ $(LDS) $(RPATH) -pthread -lprotobuf
